<?php

if(!defined("INCLUDE")) die("Nedostatečné oprávnění pro spouštění souboru.");

# --------------------------------------------------------------------------------------------------------------------------
# CLASS PROCESOR
# --------------------------------------------------------------------------------------------------------------------------
# Procesor je výchozí třída všech dat pro daný modul CMS. Procesor se využívá v controlleru, kde volá právě příslušnou metodu
# pro vykonání dané akce, metody, které mají dělat nějákou akci se od vykreslujícíh metod odlišují prefixem "action_".
# Procesor spolupracuje se třídama entity, mapper a presentation.
#
# --------------------------------------------------------------------------------------------------------------------------
# PROMĚNNÉ
# --------------------------------------------------------------------------------------------------------------------------
# Proměnné třídy slouží k uložení instancí tříd, dle kterých jsou pojmenovány. 
#
# private $presentation;
# private $entity;
# private $mapper;
#
# --------------------------------------------------------------------------------------------------------------------------
# FUNKCE
# --------------------------------------------------------------------------------------------------------------------------
#
#
# --------------------------------------------------------------------------------------------------------------------------
# INFORMACE O SOUBORU
# --------------------------------------------------------------------------------------------------------------------------
# Autor: Zechy
# Poslední úprava: 1. 12. 2012
# --------------------------------------------------------------------------------------------------------------------------

class procesor
{
	# ------------------------------------------------------------------------------------------------------------------------
	# ------------------------------------------------------------------------------------------------------------------------
	# PROMĚNNÉ
	# ------------------------------------------------------------------------------------------------------------------------
	# ------------------------------------------------------------------------------------------------------------------------
	private $presentation;
	private $entity;
	private $mapper;

	# ------------------------------------------------------------------------------------------------------------------------
	# ------------------------------------------------------------------------------------------------------------------------
	# FUNKCE
	# ------------------------------------------------------------------------------------------------------------------------
	# ------------------------------------------------------------------------------------------------------------------------
	# Konstruktor
	# ------------------------------------------------------------------------------------------------------------------------
	public function __construct()
	{
		$this->presentation = new presentation;
		$this->entity = new entity;
		$this->mapper = new mapper;
	}

	# ------------------------------------------------------------------------------------------------------------------------
	# Hlavní stránky
	# ------------------------------------------------------------------------------------------------------------------------
	# Přehled článků
	# ------------------------------------------------------------------------------------------------------------------------
	public function index()
	{
		try
		{
			isset($_GET["page"]) ? $page = $_GET["page"] : $page = 1;
			$getall = $this->mapper->getall_articles(true, $page);

			if($getall != false)
			{
				$entity = array();
				$i = 0;

				while($row = mysql_fetch_array($getall))
				{
					$entity[$i] = new entity;
					$entity[$i++]->setAll_fromDB($row);
				}

				$this->presentation->index($entity, null, null);
			}
			else
			{
				$this->presentation->index(null, null, null, "empty_result");
			}
		}
		catch(Exception $e)
		{
			$this->presentation->index(null, null, null, "thrown_exception", $e->getMessage());
		}
	}

	# ------------------------------------------------------------------------------------------------------------------------
	# Přehled kategorií
	# ------------------------------------------------------------------------------------------------------------------------
	public function category_view()
	{
		try
		{
			$getall = $this->mapper->getall_categories();

			if($getall != false)
			{
				$this->presentation->category_view($getall);
			}
			else
			{
				$this->presentation->category_view(null, "empty_result");
			}
		}
		catch(Exception $e)
		{
			$this->presentation->category_view(null, "thrown_exception", $e->getMessage());
		}
	}

	# ------------------------------------------------------------------------------------------------------------------------
	# AKCE
	# ------------------------------------------------------------------------------------------------------------------------
	# Články
	# ------------------------------------------------------------------------------------------------------------------------
	# Přidávání článků
	# ------------------------------------------------------------------------------------------------------------------------
	# Vykreslení formuláře
	public function add_article()
	{
		$this->presentation->add_article();
	}

	# ------------------------------------------------------------------------------------------------------------------------
	# Zpracování nového článku
	public function action_add_article()
	{
		try
		{
			$heading = trim($_POST["heading"]);

			$content = str_ireplace("<p>", "" , $_POST["content"]);
			$content = str_ireplace("</p>", " ", $content);
			$content = str_ireplace("&nbsp;", " ", $content);

			$content = trim($content);
			$date = trim($_POST["date"]);
			$time = trim($_POST["time"]);

			if(empty($heading))
			{
				$this->presentation->reporting("empty", "article_heading");
			}
			else if(empty($content) && isset($_POST["public"]))
			{
				$this->presentation->reporting("empty", "article_content");
			}
			else if(!empty($date) && empty($time))
			{
				$this->presentation->reporting("empty", "article_time");
			}
			else
			{
				$this->entity->setAll($_POST);
				$add = $this->mapper->add_article($this->entity);

				if($add == 2)
				{
					$this->presentation->reporting("success", "article_add");
					echo "<p align='center'><a href='index.php?modul=articles'>".$_SESSION["basic"]["article_back"]."</a></p>";
				}
				else if($add == 1)
				{
					$this->presentation->reporting("succes", "article_add");
					$this->presentation->reporting("warning", "action");
					echo "<p align='center'><a href='index.php?modul=articles'>".$_SESSION["basic"]["article_back"]."</a></p>";
				}
				else if($add == 0)
				{
					$this->presentation->reporting("warning", "article_notsave");
					echo "<p align='center'><a href='index.php?modul=articles'>".$_SESSION["basic"]["article_back"]."</a></p>";
				}
			}
		}
		catch(Exception $e)
		{
			echo "<p class='ui-state-error ui-corner-all'>".$e->getMessage()."</p>";
		}
	}

	# --------------------------------------------------------------------------------------------------------------------------
	# Editace článků
	# --------------------------------------------------------------------------------------------------------------------------
	# Vykreslení formuláře
	public function edit_article()
	{
		try
		{
			$article = $this->mapper->get_article_by_id($_GET["id"]);

			if($article != false)
			{
				$row = mysql_fetch_array($article);
				$this->entity->setAll_fromDB($row);
				$this->presentation->edit_article($this->entity);
			}
			else
			{
				$this->presentation->reporting("warning", "article_notfound");
			}
		}
		catch(Exception $e)
		{
			echo "<p class='ui-state-error ui-corner-all'>".$e->getMessage()."</p>";
		}
	}

	# --------------------------------------------------------------------------------------------------------------------------
	# Zpracování editace
	public function action_edit_article()
	{
		try
		{
			if(empty($_POST["date"]))
			{
				$_POST["date"] = $_POST["date_rezerva"];
			}

			$this->entity->setAll($_POST);
			$edit = $this->mapper->edit_article($this->entity);

			if($edit == 3)
			{
				$this->presentation->reporting("success", "article_edit");
				echo "<p align='center'><a href='index.php?modul=articles'>".$_SESSION["basic"]["article_back"]."</a></p>";
			}
			else if($edit == 2)
			{
				$this->presentation->reporting("warning", "article_edit");
				echo "<p align='center'><a href='index.php?modul=articles'>".$_SESSION["basic"]["article_back"]."</a></p>";
			}
			else if($edit == 1)
			{
				$this->presentation->reporting("warning", "action_article_edit");
				echo "<p align='center'><a href='index.php?modul=articles'>".$_SESSION["basic"]["article_back"]."</a></p>";
			}
			else if($edit == 0)
			{
				$this->presentation->reporting("warning", "action");
				echo "<p align='center'><a href='index.php?modul=articles'>".$_SESSION["basic"]["article_back"]."</a></p>";
			}
		}
		catch(Exception $e)
		{
			echo "<p class='ui-state-error ui-corner-all'>".$e->getMessage()."</p>";
		}
	}

	# --------------------------------------------------------------------------------------------------------------------------
	# Mazání článků
	# --------------------------------------------------------------------------------------------------------------------------
	# Vykreslení formuláře
	public function delete_article()
	{
		try
		{
			$article = $this->mapper->get_article_by_id($_GET["id"]);

			if($article != false)
			{
				$this->presentation->delete_article($article);
			}
			else
			{
				$this->presentation->delete_article(null, "empty_result");
			}
		}
		catch(Exception $e)
		{
			$this->presentation->delete_article(null, "thrown_exception", $e->getMessage());
		}
	}

	# --------------------------------------------------------------------------------------------------------------------------
	# Zpracování akce
	public function action_delete_article()
	{
		try
		{
			$delete_category = $this->mapper->delete_article($_POST["id"]);

			if($delete_category == 2)
			{
				$this->presentation->reporting("success", "article_delete");
				echo "<p align='center'><a href='index.php?modul=articles'>".$_SESSION["basic"]["article_back"]."</a></p>
				";
			}
			else if($delete_category == 1)
			{
					$this->presentation->reporting("success", "article_delete");
					$this->presentation->reporting("warning", "action");
				echo "<p align='center'><a href='index.php?modul=articles'>".$_SESSION["basic"]["article_back"]."</a></p>
				";
			}
			else if($delete_category == 0)
			{
				$this->presentation->reporting("warning", "article_delete");
				echo "
					<p align='center'><a href='index.php?modul=articles>".$_SESSION["basic"]["article_back"]."</a></p>
				";
			}
		}
		catch(Exception $e)
		{
			$this->presentation->delete_category(null, "thrown_exception", $e->getMessage());
		}
	}

	# --------------------------------------------------------------------------------------------------------------------------
	# Kategorie
	# --------------------------------------------------------------------------------------------------------------------------
	# Přidávání kategorií
	# --------------------------------------------------------------------------------------------------------------------------
	# Vykreslení formuláře
	public function add_category()
	{
		$this->presentation->add_category();
	}

	# --------------------------------------------------------------------------------------------------------------------------
	# Zpracování nové kategorie
	public function action_add_category()
	{
		try
		{
			$name = trim($_POST["name"]);

			if(empty($name))
			{
				$this->presentation->reporting("empty", "category_name");
			}
			else
			{
				$new_category = $this->mapper->new_category();

				if($new_category == 2)
				{
					$this->presentation->reporting("success", "category_add");
					echo "<p align='center'><a href='index.php?modul=articles&subpage=category_view'>".$_SESSION["basic"]["article_back"]."</a></p>";
				}
				else if($new_category == 1)
				{
					$this->presentation->reporting("warning", "category_add");
				}
				else if($new_category == 0)
				{
					$this->presentation->reporting("success", "category_add");
					$this->presentation->reporting("warning", "action");
					echo "<p align='center'><a href='index.php?modul=articles&subpage=category_view'>".$_SESSION["basic"]["article_back"]."</a></p>";
				}
			}
		}
		catch(Exception $e)
		{
			echo "<p class='ui-state-error ui-corner-all'>".$e->getMessage()."</p>";
		}
	}

	# --------------------------------------------------------------------------------------------------------------------------
	# Editace kategorií
	# --------------------------------------------------------------------------------------------------------------------------
	# Vykreslení formuláře
	public function edit_category()
	{
		try
		{
			$get_category = $this->mapper->get_category_by_id($_GET["id"]);

			if($get_category != false)
			{
				$this->presentation->edit_category($get_category);
			}
			else
			{
				$this->presentation->edit_category(null, "empty_result");
			}
		}
		catch(Exception $e)
		{
			$this->presentation->edit_category(null, "thrown_exception", $e->getMessage());
		}
	}

	# --------------------------------------------------------------------------------------------------------------------------
	# Zpracování akce
	public function action_edit_category()
	{
		try
		{
			$edit_category = $this->mapper->edit_category($_POST["name"], $_POST["note"], $_POST["id"]);

			if($edit_category == 3)
			{
				$this->presentation->reporting("success", "category_edit");
				echo "
					<p align='center'><a href='index.php?modul=articles&subpage=category_view'>".$_SESSION["basic"]["article_back"]."/a></p>
				";
			}
			else if($edit_category == 2)
			{
				$this->presentation->reporting("warning", "category_edit");
			}
			else if($edit_category == 1)
			{
				$this->presentation->reporting("warning", "action_category_edit");
			}
			else if($edit_category == 0)
			{
				$this->presentation->reporting("warning", "action");
			}
		}
		catch(Exception $e)
		{
			echo "<p class='ui-state-error ui-corner-all'>".$e->getMessage()."</p>";
		}
	}

	# --------------------------------------------------------------------------------------------------------------------------
	# Mazání kategorií
	# --------------------------------------------------------------------------------------------------------------------------
	public function delete_category()
	{
		try
		{
			$category = $this->mapper->get_category_by_id($_GET["id"]);
			
			if($category != false)
			{
				$this->presentation->delete_category($category);
			}
			else
			{
				$this->presentation->delete_category(null, "empty_result");
			}
		}
		catch(Exception $e)
		{
			$this->presentation->delete_category(null, "thrown_exception", $e->getMessage());
		}
	}

	public function action_delete_category()
	{
		try
		{
			$delete_category = $this->mapper->delete_category($_POST["id"]);

			if($delete_category == 3)
			{
				echo "
					<p class='uspech ui-corner-all'><img src='theme/ikonky/tick.png' align='absmiddle'> Kategorie byla úspěšně označena za smazanou.</p>
					<p align='center'><a href='index.php?modul=articles&subpage=category_view'>".$_SESSION["basic"]["article_back"]."</a></p>
				";
			}
			else if($delete_category == 2)
			{
				echo "
					<p class='uspech ui-corner-all'><img src='theme/ikonky/tick.png' align='absmiddle'> Kategorie byla úspěšně označena za smazanou.</p>
					<p class='ui-state-highlight ui-corner-all'><img src='theme/ikonky/error.png' align='absmiddle'> <b>Chyba Action logeru</b>: Nepodařilo se zapsat záznam o smazání. Kategorie ovšem byla smazána.</p>
					<p align='center'><a href='index.php?modul=articles&subpage=category_view'>".$_SESSION["basic"]["article_back"]."</a></p>
				";
			}
			else if($delete_category == 1)
			{
				echo "
					<p class='ui-state-highlight ui-corner-all'><img src='theme/ikonky/error.png' align='absmiddle'> <b>Chyba zápisu</b>: NEpodařilo se označit kategorii za smazanou.</p>
					<p align='center'><a href='index.php?modul=articles&subpage=category_view'>".$_SESSION["basic"]["article_back"]."</a></p>
				";
			}
			else if($delete_category == 0)
			{
				echo "
					<p class='ui-state-highlight ui-corner-all'><img src='theme/ikonky/error.png' align='absmiddle'> <b>Chyba zápisu</b>: Nepodařilo se přesunout kategorie do výchozí kategorie.</p>
				";
			}
		}
		catch(Exception $e)
		{
			$this->presentation->delete_category(null, "thrown_exception", $e->getMessage());
		}
	}
}

?>